home *** CD-ROM | disk | FTP | other *** search
- Imports System.ComponentModel
-
- ' Let the form know that this control will add the UserRole property.
-
- <ProvideProperty("UserRole", GetType(Control))> _
- Public Class UserPropExtender
- Inherits System.ComponentModel.Component
-
- Implements IExtenderProvider
-
- ' This function must return True for all controls that can be extended with
- ' the UserRole property
- Public Function CanExtend(ByVal extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
- ' extend all controls except this one
- ' (not really necessary in this case, because this is a Component, not a control.)
- If Not (TypeOf extendee Is UserPropExtender) Then
- Return True
- End If
- End Function
-
- ' This is the Hashtable that holds the association between controls
- ' and their UserRole.
- Dim userRoleValues As New Hashtable()
-
- ' These are the Get/Set methods related to the property being added.
-
- Function GetUserRole(ByVal ctrl As Control) As String
- ' Check if there is a property associated to this control.
- Dim value As Object = userRoleValues(ctrl)
- ' Return the value found, or an empty string
- If value Is Nothing Then
- Return ""
- Else
- Return value.ToString
- End If
- End Function
-
- Sub SetUserRole(ByVal ctrl As Control, ByVal value As String)
- ' in case it is passed Nothing.
- If Value Is Nothing Then Value = ""
-
- If Value.Length = 0 And userRoleValues.Contains(ctrl) Then
- ' remove the control from the hashtable
- userRoleValues.Remove(ctrl)
- ' remove event handlers, if any
- ' ...
- ElseIf Value.Length > 0 Then
- If Not userRoleValues.Contains(ctrl) Then
- ' add any event handlers here
- ' ...
- End If
- ' assign the new value.
- userRoleValues.Item(ctrl) = Value
- ' refresh this control.
- SetControlVisibility(ctrl)
- End If
- End Sub
-
- ' this property can be assigned the name of the current user.
-
- Dim m_CurrentUserRole As String
-
- Property CurrentUserRole() As String
- Get
- Return m_CurrentUserRole
- End Get
- Set(ByVal Value As String)
- m_CurrentUserRole = Value
- RefreshAllControls()
- End Set
- End Property
-
- ' hide/show all controls based on their UserRole property
-
- Sub RefreshAllControls()
- Dim ctrl As Control
- For Each ctrl In userRoleValues.Keys
- SetControlVisibility(ctrl)
- Next
- End Sub
-
- ' hide/show a single control based on its UserRole property
-
- Private Sub SetControlVisibility(ByVal ctrl As Control)
- ' do nothing if no user has been defined
- If CurrentUserRole = "" Then Exit Sub
- ' do nothing if the control isn't in the hash table
- If Not userRoleValues.Contains(ctrl) Then Exit Sub
-
- ' get the value in the hash table
- Dim value As String = userRoleValues(ctrl).ToString
-
- ' check whether the current user is one of the defined users
- If InStr(";" & value & ";", ";" & CurrentUserRole & ";", CompareMethod.Text) > 0 Then
- ctrl.Visible = True
- Else
- ctrl.Visible = False
- End If
- End Sub
-
- End Class
-